﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace JSTT_DATA_ENTITY.XMLDatas
{
    /// <summary>
    /// 对于DATATYPE RULE XML数据.
    /// 此类描述了XML下<DataItems><DataItem><DataItemProperty></DataItemProperty></DataItem></DataItems>节点
    /// </summary>
    public class DataItemProperty
    {
        private bool _isEnforce;            // 存在强制值
        private string _enforceVal;         // 强制值的数据
        private bool _isDefineColumn;       // 是否在数据库中定义Column
        private string _defineColumnName;   // 是否在数据库中定义Column
        private int _processIndex;          // 处理的顺序
        private int _maxValue;              // 此字段最大值
        private int _minValue;              // 此字段的最小值
        private int _ampFactor;             // 数据倍率

        public DataItemProperty()
        {
            
        }

        public DataItemProperty(bool isEnforce, string enforceVal, bool isDefineColumn, string defineColumnName,
            int processIndex,int maxValue,int minValue,int ampFactor)
        {
            this._isEnforce = isEnforce;
            this._enforceVal = enforceVal;
            this._isDefineColumn = isDefineColumn;
            this._defineColumnName = defineColumnName;
            this._processIndex = processIndex;
            this._ampFactor = ampFactor;
            this._maxValue = maxValue;
            this._minValue = minValue;
        }

        [XmlAttribute("IsEnforce")]
        public bool IsEnforce
        {
            get { return _isEnforce; }
            set { _isEnforce = value; }
        }

        [XmlAttribute("EnforceVal")]
        public string EnforceVal
        {
            get { return _enforceVal; }
            set { _enforceVal = value; }
        }

        [XmlAttribute("IsDefineColumn")]
        public bool IsDefineColumn
        {
            get { return _isDefineColumn; }
            set { _isDefineColumn = value; }
        }

        [XmlAttribute("DefineColumnName")]
        public string DefineColumnName
        {
            get { return _defineColumnName; }
            set { _defineColumnName = value; }
        }

        [XmlAttribute("ProcessIndex")]
        public int ProcessIndex
        {
            get { return _processIndex; }
            set { _processIndex = value; }
        }

        [XmlAttribute("MaxValue")]
        public int MaxValue
        {
            get { return _maxValue; }
            set { _maxValue = value; }
        } 

        [XmlAttribute("MinValue")]
        public int MinValue
        {
            get { return _minValue; }
            set { _minValue = value; }
        }

        [XmlAttribute("AmpFactor")]
        public int AmpFactor
        {
            get { return _ampFactor; }
            set { _ampFactor = value; }
        } 
    }

    /// <summary>
    /// DataRule下各个Data字段属性
    /// <DataItem Name="起始位1" DataType="Number" ByteStart=0 BitStart=0 Length=1 Unit="Byte">
    ///     <DataItemProperty isEnforce="Yes" EnforceVal="68H".../> 
    /// </DataItem>
    /// </summary>
    public class DataRuleItem
    {
        private string _itemRuleName;
        private string _itemDataType;
        private int _byteStart;                         // 字段在数据中字节的起始位，从0计数
        private int _bitStart;                          // 字段在数据中比特的起始位，从0计数
        private int _byteLength;                        // 字段在数据中字节的长度
        private int _bitLength;                         // 字段在数据字段中比特位的长度
        //private string _itemDataUnit;                   // 数据单位值
        private DataItemProperty _itemDataProperty;     // DataItemProperty节点属性

        public DataRuleItem()
        {
           _itemDataProperty = new DataItemProperty(); 
        }

        public DataRuleItem(string itemRuleName, string itemDataType, int byteStart, int bitStart, int byteLength,
            int bitLength,
            //string itemDataUnit,
            bool isEnforce, string enforceVal, bool isDefineColumn, string defineColumnName,
            int processIndex, int maxValue, int minValue, int ampFactor)

            :this(itemRuleName,itemDataType,byteStart,bitStart,byteLength,bitLength, //itemDataUnit,
            new DataItemProperty(isEnforce,enforceVal,isDefineColumn,defineColumnName,processIndex,maxValue,minValue,ampFactor))
        {
        }

        public  DataRuleItem(string itemRuleName, string itemDataType, int byteStart, int bitStart, int byteLength,
            int bitLength,
            //string itemDataUnit,
            DataItemProperty itemDataProperty)
        {
            this._itemRuleName = itemRuleName;
            this._itemDataType = itemDataType;
            this._byteStart = byteStart;
            this._bitStart = bitStart;
            this._byteLength = byteLength;
            this._bitLength = bitLength;
            //this._itemDataUnit = itemDataUnit;
            this._itemDataProperty = itemDataProperty;
        }

        [XmlAttribute("ItemRuleName")]
        public string ItemRuleName
        {
            get { return _itemRuleName; }
            set { _itemRuleName = value; }
        }

        [XmlAttribute("ItemDataType")]
        public string ItemDataType 
        {
            get { return _itemDataType; }
            set { _itemDataType = value; }
        }

        [XmlAttribute("ByteStart")]
        public int ByteStart
        {
            get { return _byteStart; }
            set { _byteStart = value; }
        }

        [XmlAttribute("BitStart")]
        public int BitStart
        {
            get { return _bitStart; }
            set { _bitStart = value; }
        }

        [XmlAttribute("ByteLength")]
        public int ByteLength
        {
            get { return _byteLength; }
            set { _byteLength = value; }
        }

        [XmlAttribute("BitLength")]
        public int BitLength
        {
            get { return _bitLength; }
            set { _bitLength = value; }
        }

        //[XmlAttribute("ItemDataUnit")]
        //public string ItemDataUnit 
        //{
        //    get { return _itemDataUnit; }
        //    set { _itemDataUnit = value; }
        //}

        [XmlElement("DataItemProperty")]
        public DataItemProperty ItemProperty
        {
            get { return _itemDataProperty; }
            set { _itemDataProperty = value; }
        }
    }


    /// <summary>
    /// Represent DataTypeRule.DF_TableInfo Element
    /// <DF_DataRules>
    ///     <DATA Name="起始位1" DataType="Number" ByteStart=0 BitStart=0 Length=1 Unit="Byte">
    ///         <DataValue IsEnforce="Yes" EnForceVal="68H" ValMode="Hex" /> 
    ///         <ColumnDefine IsDefineColumn="No" />
    ///     </DATA>
    ///     <DATA ... />
    /// </DF_DataRules>
    /// </summary>
    public class JSTT_DataRuleItems
    {
        private List<DataRuleItem> dtRuleItems;

        public JSTT_DataRuleItems()
        {
            dtRuleItems = new List<DataRuleItem>();
        }

        public void AddDataRuleItem(string itemRuleName, string itemDataType, int byteStart, int bitStart,
            int byteLength,
            int bitLength,
            string itemDataUnit,
            bool isEnforce, string enforceVal, bool isDefineColumn, string defineColumnName,
            int processIndex,int maxValue,int minValue,int ampFactor)
        {
            dtRuleItems.Add(new DataRuleItem(itemRuleName, itemDataType, byteStart, bitStart, byteLength, bitLength, //itemDataUnit,
             isEnforce, enforceVal, isDefineColumn, defineColumnName, processIndex,maxValue,minValue,ampFactor)); 
        }

        public void InsertDataRuleItems(IEnumerable<DataRuleItem> dtRuleItems)
        {
            RuleItems = dtRuleItems.ToArray();
        }

        [XmlArrayItem("DataItem", typeof (DataRuleItem))]
        [XmlArray("DataItems")]
        public DataRuleItem[] RuleItems
        {
            get { return dtRuleItems.ToArray(); }
            set {
                foreach (DataRuleItem dataRuleItem in value)
                {
                    dtRuleItems.Add(dataRuleItem);
                }
            }
        }
    }




    public class ExptItem
    {
        private string _itemRuleName;
        private string _itemDataType;
        private int _byteStart;                         // 字段在数据中字节的起始位，从0计数
        private int _bitStart;                          // 字段在数据中比特的起始位，从0计数
        private int _byteLength;                        // 字段在数据中字节的长度
        private int _bitLength;                         // 字段在数据字段中比特位的长度
        private string _itemDataUnit;                   // 数据单位值

        public ExptItem()
        {
        }


        public ExptItem(string itemRuleName, string itemDataType, int byteStart, int bitStart, int byteLength,
            int bitLength,
            string itemDataUnit)
        {
            this._itemRuleName = itemRuleName;
            this._itemDataType = itemDataType;
            this._byteStart = byteStart;
            this._bitStart = bitStart;
            this._byteLength = byteLength;
            this._bitLength = bitLength;
            this._itemDataUnit = itemDataUnit;
        }

        [XmlAttribute("ItemRuleName")]
        public string ItemRuleName
        {
            get { return _itemRuleName; }
            set { _itemRuleName = value; }
        }

        [XmlAttribute("ItemDataType")]
        public string ItemDataType
        {
            get { return _itemDataType; }
            set { _itemDataType = value; }
        }

        [XmlAttribute("ByteStart")]
        public int ByteStart
        {
            get { return _byteStart; }
            set { _byteStart = value; }
        }

        [XmlAttribute("BitStart")]
        public int BitStart
        {
            get { return _bitStart; }
            set { _bitStart = value; }
        }

        [XmlAttribute("ByteLength")]
        public int ByteLength
        {
            get { return _byteLength; }
            set { _byteLength = value; }
        }

        [XmlAttribute("BitLength")]
        public int BitLength
        {
            get { return _bitLength; }
            set { _bitLength = value; }
        }

        [XmlAttribute("ItemDataUnit")]
        public string ItemDataUnit
        {
            get { return _itemDataUnit; }
            set { _itemDataUnit = value; }
        }

    }
    /// <summary>
    /// Represent DataTypeRule.JSTT_EXPTDataItems Element 
    /// <DF_TableInfo>
    ///    <JSTT_EXPTDataItems>
    ///         <ExptItem  ItemDataUnit="bit" BitLength="1" ByteLength="1" BitStart="0" ByteStart="84" ItemDataType="Number" ItemRuleName="主开关异常" itemUnitName=""></ExptItem>
    ///    </JSTT_EXPTDataItems>
    /// </DF_TableInfo>
    /// </summary>
    public class JSTT_EXPTDataItems
    {
        private List<ExptItem> dtExptItems;

        public JSTT_EXPTDataItems()
        {
            dtExptItems = new List<ExptItem>();
        }

        public void AddEXPTDataItem(string itemRuleName, string itemDataType, int byteStart, int bitStart,
            int byteLength,
            int bitLength,
            string itemDataUnit )
        {
            dtExptItems.Add(new ExptItem(itemRuleName, itemDataType, byteStart, bitStart, byteLength, bitLength, itemDataUnit)); 
        }


        //[XmlArray("JSTT_EXPTDataItems")]
        [XmlArrayItem("ExptItem", typeof(ExptItem))]
        [XmlArray("ExptItems")]
        public ExptItem[] ExptItems
        {
            get { return dtExptItems.ToArray(); }
            set {
                foreach (ExptItem exptItem in value)
                {
                    dtExptItems.Add(exptItem);
                }
            }
        }
    }
}
